package com.thinkcube.edutainment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;

import com.thinkcube.edutainment.adapters.ImageAdapterCircleGallery;
import com.thinkcube.edutainment.client.CourseClient;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.fragment.HomeFragment_;
import com.thinkcube.edutainment.service.SPreferences_;

@EViewGroup
public class SelectedItemEnlarged extends Gallery {
	// 3 default constructors
	Context mContext;
	List<CourseData> allcourses;

	public SelectedItemEnlarged(Context context) {
		super(context);
		mContext = context;
		// initAdapter();
	}

	public SelectedItemEnlarged(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		// initAdapter();
	}

	public SelectedItemEnlarged(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		// initAdapter();
	}

	// List<CourseData> placeList;
	@RestService
	CourseClient courseClient;
	@Bean
	CourseDto courseDto;
	
	@Pref
	SPreferences_ pref;

	@Background
	@AfterViews
	void initAdapter() {

		courseDto.setAuthToken(EdutainmentConstant.APP_ID);
		courseDto.setUserId(pref.getId().get());
		courseDto.setFilterBy("featured");
		new ReachabilityTest(getContext(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {
				
				try {
					placeListUI(
							mContext,
							courseClient.getFiltercourses(
									courseDto.getMultiValuedMap())
									.getCourseList());
				} catch (Exception e) {
					
					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {
				
				showMessge("not connected ");
			}
		});

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
	}

	@UiThread
	void placeListUI(Context mContext, List<CourseData> filtercourses) {
		initiateAdapter(mContext, filtercourses);
	}

	View lastSelectedView = null;

	private void initiateAdapter(Context context, List<CourseData> courseList) {

		this.allcourses = courseList;
		this.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (lastSelectedView != null) {
					lastSelectedView.setLayoutParams(new Gallery.LayoutParams(
							330, 390));
					// lastSelectedView.findViewById(R.id.course_image).setLayoutParams(new
					// RelativeLayout.LayoutParams(330,150));
				}
				arg1.setLayoutParams(new Gallery.LayoutParams(380, 440)); // selected
																			// size
				// arg1.findViewById(R.id.course_image).setLayoutParams(new
				// RelativeLayout.LayoutParams(330,200));
				lastSelectedView = arg1;

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		setAdapter(new ImageAdapterCircleGallery(context, courseList));
        HomeFragment_.progress.setVisibility(View.GONE);
		// To select the xSelected one -> 0 is the first.
		int xSelected = 0;
		// To make the view go to the middle of our 'endless' array
		// setSelection(Integer.MAX_VALUE / 2 + (Integer.MAX_VALUE / 2) % 5 - 1
		// + xSelected);
		setSelection(2);
	}

	public List<CourseData> getCourselist() {

		return allcourses;
	}

	@UiThread
	void showDialog() {
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:

					break;

				case DialogInterface.BUTTON_NEGATIVE:

					break;
				}
			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
		builder.setMessage("Not reachable")
				.setPositiveButton("Yes", dialogClickListener)
				.setNegativeButton("No", dialogClickListener).show();
	}

}
